package et.item;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import et.common.ElectricThaum;
import ic2.api.item.ElectricItem;
import ic2.api.item.IElectricItem;
import ic2.api.item.IItemHudInfo;
import ic2.core.IC2;
import ic2.core.item.armor.ItemArmorNanoSuit;
import ic2.core.item.armor.ItemArmorQuantumSuit;
import ic2.core.util.StackUtil;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by winston_wang on 2017/7/9.
 */
public class ETItemVoidSaber extends Item implements IElectricItem,IItemHudInfo {
    private int soundTicker;
    public int maxCharge;
    public int transferLimit;
    public int tier;
    public double operationEnergyCost;
    public ETItemVoidSaber(double operationEnergyCost, int maxCharge, int transferLimit, int tier){
        this.maxCharge=maxCharge;
        this.transferLimit=transferLimit;
        this.tier=tier;
        this.operationEnergyCost=operationEnergyCost;
        setNoRepair();
        setMaxDamage(27);
        setFull3D();
        setMaxStackSize(1);
    }
    public ETItemVoidSaber(){
        this(10,200000,500,1);
        setTextureName("et:itemVoidSaber");
        setUnlocalizedName("voidSaber");
        setCreativeTab(ElectricThaum.tabET);
    }
    @Override
    public void addInformation(ItemStack itemStack, EntityPlayer player, List info, boolean b) {
        info.add(StatCollector.translateToLocal("ic2.item.tooltip.PowerTier") + " " + this.tier);
    }
    @Override
    public List<String> getHudInfo(ItemStack itemStack) {
        LinkedList info = new LinkedList();
        info.add(ElectricItem.manager.getToolTip(itemStack));
        info.add(StatCollector.translateToLocal("ic2.item.tooltip.PowerTier") + " " + this.tier);
        return info;
    }
    public float getDigSpeed(ItemStack itemStack, Block block, int meta) {
        NBTTagCompound nbtData = StackUtil.getOrCreateNbtData(itemStack);
        if(nbtData.getBoolean("active")) {
            ++this.soundTicker;
            if(this.soundTicker % 4 == 0) {
                IC2.platform.playSoundSp(this.getRandomSwingSound(), 1.0F, 1.0F);
            }

            return 4.0F;
        } else {
            return 1.0F;
        }
    }
    @SideOnly(Side.CLIENT)
    public EnumRarity getRarity(ItemStack stack) {
        return EnumRarity.uncommon;
    }
    @Override
    public boolean canProvideEnergy(ItemStack itemStack) {
        return false;
    }
    @Override
    public Item getChargedItem(ItemStack itemStack) {
        return this;
    }
    @Override
    public Item getEmptyItem(ItemStack itemStack) {
        return this;
    }
    @Override
    public double getMaxCharge(ItemStack itemStack) {
        return maxCharge;
    }
    @Override
    public int getTier(ItemStack itemStack) {
        return tier;
    }
    @Override
    public double getTransferLimit(ItemStack itemStack) {
        return transferLimit;
    }
    public String getRandomSwingSound() {
        switch(IC2.random.nextInt(3)) {
            case 1:
                return "Tools/Nanosabre/NanosabreSwing2.ogg";
            case 2:
                return "Tools/Nanosabre/NanosabreSwing3.ogg";
            default:
                return "Tools/Nanosabre/NanosabreSwing1.ogg";
        }
    }
    @Override
    public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase source) {
        if(IC2.platform.isSimulating()) {
            if(!(source instanceof EntityPlayer) || MinecraftServer.getServer().isPVPEnabled() || !(target instanceof EntityPlayer)) {
                for(int i = 0; i < 4 && ElectricItem.manager.canUse(stack, 5.0D); ++i) {
                    ElectricItem.manager.use(stack, 5.0D, source);
                    ItemStack armor = target.getEquipmentInSlot(i + 1);
                    if (armor != null) {
                        double amount = 0.0D;
                        if (armor.getItem() instanceof ItemArmorNanoSuit) {
                            amount = 5.0D;
                        } else if (armor.getItem() instanceof ItemArmorQuantumSuit) {
                            amount = 200.0D;
                        }
                        if (amount > 0.0D) {
                            ElectricItem.manager.discharge(armor, amount, this.tier, true, false, false);
                            if (!ElectricItem.manager.canUse(armor, 1.0D)) {
                                target.setCurrentItemOrArmor(i + 1, (ItemStack) null);
                            }
                        }
                    }
                    target.addPotionEffect(new PotionEffect(2, 1000, 2));
                    target.addPotionEffect(new PotionEffect(9, 1000, 2));
                    target.addPotionEffect(new PotionEffect(18, 1000, 2));
                }
            }
        }
        if(IC2.platform.isRendering()) {
            IC2.platform.playSoundSp(this.getRandomSwingSound(), 1.0F, 1.0F);
        }
        return true;
    }

    @Override
    public ItemStack onItemRightClick(ItemStack p_77659_1_, World p_77659_2_, EntityPlayer p_77659_3_) {
        ElectricItem.manager.charge(p_77659_1_,0,tier,true,false);
        return p_77659_1_;
    }

    @Override
    public Multimap getAttributeModifiers(ItemStack stack) {
        int dmg = ElectricItem.manager.canUse(stack, 5.0D) ? 40:4;
        HashMultimap ret1 = HashMultimap.create();
        ret1.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Tool modifier", (double)dmg, 0));
        return ret1;
    }
}


